//
//  NotificationViewController.m
//  XRSquare
//
//  Created by UaenaMo on 2018/8/16.
//  Copyright © 2018年 UaenaMo. All rights reserved.
//

#import "NotificationViewController.h"
#import "NotificationTableViewCell.h"
@interface NotificationViewController ()<UITableViewDataSource,UITableViewDelegate,NotificationDelegate>
{
    UITableView * contentTableView;
    NSArray * dataArr;

}

@end

@implementation NotificationViewController

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

-(void)requestData
{
    [NetRequestCenter messagelistResult:^(BOOL success, NSDictionary *resultDic) {
        if (success) {
            if ([[resultDic objectForKey:@"data"]objectForKey:@"Detail"])
            {
                dataArr = [[resultDic objectForKey:@"data"]objectForKey:@"Detail"];
                [contentTableView reloadData];
            }
        }
    }];
}

-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    self.title = @"消息";
    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
    CGRect rectNav = self.navigationController.navigationBar.frame;
    
    contentTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, rectStatus.size.height + rectNav.size.height, ScreenWidth, ScreenHeight - rectStatus.size.height - rectNav.size.height) style:UITableViewStylePlain];
    contentTableView.delegate = self;
    contentTableView.dataSource = self;
    contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:contentTableView];
    
    [self requestData];
}


#pragma mark - tableview

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataArr count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [NotificationTableViewCell height];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifer = @"identifer";
    NotificationTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (!cell) {
        cell = [[NotificationTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    }
    cell.delegate = self;
    [cell loadData:[dataArr objectAtIndex:indexPath.row]];
    return cell;
}

-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        NSDictionary * content = [dataArr objectAtIndex:indexPath.row];
        NSLog(@"%@",content);
        
        [NetRequestCenter delmessagewithuid:@[[content objectForKey:@"Uid"]] result:^(BOOL success, NSDictionary *resultDic) {
            if (success)
            {
                [self requestData];
            }
        }];
    }
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

-(void)acceptAction:(NSDictionary *)content
{
    [NetRequestCenter agreefrienduid:[content objectForKey:@"AppFriendId"] type:@"1" result:^(BOOL success, NSDictionary *resultDic) {
        if (success)
        {
            [SVProgressHUD showSuccessWithStatus:@"接受成功"];
            [self requestData];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:[resultDic objectForKey:@"message"]];
        }
    }];
}

-(void)denileAction:(NSDictionary *)content
{
    [SVProgressHUD show];
    [NetRequestCenter agreefrienduid:[content objectForKey:@"AppFriendId"] type:@"0" result:^(BOOL success, NSDictionary *resultDic) {
        if (success) {
            [SVProgressHUD showSuccessWithStatus:@"拒绝成功"];
            [self requestData];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:[resultDic objectForKey:@"message"]];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
